classdef Utils
   
    
    properties
        
    end
    
    methods
        
      
        
       
    end
    
     methods (Static)
       function arr=convertToArray(intArray, mode)
            if nargin==0
             return
            end
            if nargin==1
             mode='min';
            end
           size=length(intArray);
           arr=zeros(1,size);
           for i=1:size
				val_class=class(intArray(i));	
				if strcmp(val_class,'double')
				  arr=intArray;
				  return;
				end
				
				if strcmp(val_class,'Interval')
                    if length(intArray(i).imin)~=length(arr(i))
                        intArray(i)=Interval(0,0,'');
                    end
					if(strcmp(mode,'min'))
						arr(i)=intArray(i).imin;
					end
					if(strcmp(mode,'max'))
						arr(i)=intArray(i).imax;
                    end
                    if(strcmp(mode,'middle'))
						arr(i)=abs(intArray(i).imin+(intArray(i).imax-intArray(i).imin)/2);
					end
					if(strcmp(mode,'random'))
					   arr(i)=getRandomFromInterval(intArray(i));
					end
				end
				
				if strcmp(val_class,'AffineForm')
				   interv=intArray(i).convertToInterval;
                   if length(interv.imin)~=length(arr(i))
                        interv=Interval(0,0,'');
                    end
				   if(strcmp(mode,'min'))
						arr(i)=interv.imin;
					end
					if(strcmp(mode,'max'))
						arr(i)=interv.imax;
					end
					if(strcmp(mode,'random'))
					   arr(i)=Utils.getRandomFromInterval(interv);
					end
				end
           end
		  	
		   
		   
		   
         end  
        function ret=getRandomFromInterval(interval)
				size= length(interval.imin);
				diff=interval.imax-interval.imin;
				ret=rand(1,size).*diff+interval.imin;
		end
		
         function ret=minValue(intArray)
                 arr=Utils.convertToArray(intArray,'min');
			     ret=min(arr);
         end
         
         function C=in(X,Y)
             val_class=class(Y);
			 if strcmp(val_class,'Interval')
                 X=Interval.prepare(X);
                if X.imax<Y.imax && X.imin>Y.imin
                    C=1;
                    return;
                end
             end
             C=0;
         end
         function C=ine(X,Y)
             val_class=class(Y);
			 if strcmp(val_class,'Interval')
                 X=Interval.prepare(X);
                if X.imax<=Y.imax && X.imin>=Y.imin
                    C=1;
                    return;
                end
             end
             C=0;
         end
         
			function affineArr=convertToAffineArray(intArray)
				 if nargin==0
				    affineArr=[];
					return
				end
				size=length(intArray);
				for i=1:size
					affineArr(i)=AffineForm(intArray(i));
				end
			end		
          
		  function answer=reduce(funct, objectArray)
			arrSize=objectArray.length;
			first=objectArray(1);
			for i=2:arrSize
               second=objectArray(i);
			   answer=funct(first,second);
			   first=answer;
			end
		  end
		   
     end
     
    
end